package com.small.nacos.sys.utils;

import com.small.nacos.sys.env.EnvUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

public class PropertyUtil implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyUtil.class);

    /**
     * 通知连接超时时间
     */
    private static int notifyConnectTimeout = 100;
    /**
     * 通知Socket超时时间
     */
    private static int notifySocketTimeout = 200;

    /**
     * 是否是嵌入式存储
     */
    private static boolean embeddedStorage = EnvUtil.getStandaloneMode();

    /**
     * 是否是外部存储（mysql）
     */
    private static boolean useExternalDB = false;

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        loadSetting();
    }

    private void loadSetting() {
        try {
            setNotifyConnectTimeout(Integer.parseInt(EnvUtil.getProperty("notifyConnectTimeout", "100")));

            setNotifySocketTimeout(Integer.parseInt(EnvUtil.getProperty("notifySocketTimeout", "200")));

            //设置是否使用外部数据库
            setUseExternalDB("mysql".equalsIgnoreCase(getString("spring.datasource.platform", "")));

            //是否使用外部数据库
            if (isUseExternalDB()) {
                setEmbeddedStorage(false);
            } else {
                boolean embeddedStorage = PropertyUtil.embeddedStorage || Boolean.getBoolean("embeddedStorage");
                //设置是否使用嵌入式数据库
                setEmbeddedStorage(embeddedStorage);

                //如果不使用嵌入式数据库就使用外部数据库
                if (!embeddedStorage) {
                    setUseExternalDB(true);
                }
            }
        } catch (Exception e) {
            LOGGER.error("read application.properties failed", e);
            throw e;
        }
    }

    public static int getNotifyConnectTimeout() {
        return notifyConnectTimeout;
    }

    public static void setNotifyConnectTimeout(int notifyConnectTimeout) {
        PropertyUtil.notifyConnectTimeout = notifyConnectTimeout;
    }

    public static int getNotifySocketTimeout() {
        return notifySocketTimeout;
    }

    public static void setNotifySocketTimeout(int notifySocketTimeout) {
        PropertyUtil.notifySocketTimeout = notifySocketTimeout;
    }

    public static boolean isEmbeddedStorage() {
        return embeddedStorage;
    }

    public static void setEmbeddedStorage(boolean embeddedStorage) {
        PropertyUtil.embeddedStorage = embeddedStorage;
    }

    public static boolean isUseExternalDB() {
        return useExternalDB;
    }

    public static void setUseExternalDB(boolean useExternalDB) {
        PropertyUtil.useExternalDB = useExternalDB;
    }

    private String getString(String key, String defaultValue) {
        String value = getProperty(key);
        if (value == null) {
            return defaultValue;
        }
        LOGGER.info("{}:{}", key, value);
        return value;
    }

    public String getProperty(String key) {
        return EnvUtil.getProperty(key);
    }

}
